package com.ruoyi.client.service.impl;


import com.ruoyi.client.domain.vo.request.ClientRewardExchangeRequestVo;
import com.ruoyi.client.domain.vo.response.ClientCreditRecordResponseVo;
import com.ruoyi.client.domain.vo.response.ClientRewardIndexResponseVo;
import com.ruoyi.client.mapper.ClientRewardMapper;
import com.ruoyi.client.mapper.ClientUserTMapper;
import com.ruoyi.client.mapper.UserShippingAddressMapper;
import com.ruoyi.client.service.IRewardService;
import com.ruoyi.common.core.domain.entity.Reward;
import com.ruoyi.common.core.domain.entity.RewardExchange;
import com.ruoyi.common.core.domain.entity.UserT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 奖品管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-21
 */
@Service
public class ClientRewardServiceImpl implements IRewardService
{
    @Autowired
    private ClientRewardMapper rewardMapper;

    @Autowired
    private ClientUserTMapper userTMapper;
    
    @Autowired
    private UserShippingAddressMapper addressMapper;

    /**
     * 查询奖品功能首页
     *
     * @return 奖品功能首页
     */
    @Override
    public ClientRewardIndexResponseVo getRewardIndex(String userId) {
        // 1.获取用户剩余积分
        UserT user = userTMapper.getUser(userId);
        int userCredit= Math.toIntExact(user.getCredit());
        // 2.获取奖品列表
        List<Reward> rewardList = rewardMapper.getRewardList();
        // 3.封装返回结果
        ClientRewardIndexResponseVo rewardIndexResponseVo = new ClientRewardIndexResponseVo();
        rewardIndexResponseVo.setUserCredit(userCredit);
        rewardIndexResponseVo.setRewardList(rewardList);
        return rewardIndexResponseVo;
    }
    /**
     * 获取奖品详情
    * */

    /**
     *
     * 兑换奖品
    * */
    @Override
    @Transactional
    public int exchangeReward(ClientRewardExchangeRequestVo requestVo) {
        // 1.获取用户剩余积分
        UserT user = userTMapper.getUser(requestVo.getUserId());
        int userCredit = Math.toIntExact(user.getCredit());
        // 2.获取兑换所需积分
        Reward reward = rewardMapper.getReward(requestVo.getRewardId());
        Long price = reward.getPrice();
        // 2.1 如果积分不足，则返回-1
        if(userCredit-price*requestVo.getCount()<0){
            return -1;
        }
        // 2.2 如果积分足够
        else {
            // 2.2.1 校验库存是否足够，如果库存不足，则返回-2
            if(reward.getRemain()-requestVo.getCount()<0){
                return -2;
            }
            // 2.2.2 如果选择邮寄方式，验证收货地址是否存在
            if (requestVo.getReceiveType() != null && requestVo.getReceiveType() == 2) {
                if (requestVo.getAddressId() == null) {
                    return -3; // 收货地址不存在
                }
                // 验证地址是否存在且属于该用户
                if (addressMapper.selectUserAddressById(requestVo.getAddressId()) == null) {
                    return -3; // 收货地址不存在
                }
            }
            // 2.2.3 如果库存足够，则扣减用户积分，扣减库存，记录兑换记录 返回 1
            userTMapper.updateUserCredit(requestVo.getUserId(), (int) (-price*requestVo.getCount()));
            rewardMapper.updateRewardRemain(requestVo.getRewardId(),-requestVo.getCount());
            RewardExchange rewardExchange = new RewardExchange();
            rewardExchange.setRewardId((long) requestVo.getRewardId());
            rewardExchange.setUserId(requestVo.getUserId());
            rewardExchange.setAmount((long) requestVo.getCount());
            rewardExchange.setReceiveType(requestVo.getReceiveType());
            rewardExchange.setAddressId(requestVo.getAddressId());
            rewardMapper.insertExchangeRecord(rewardExchange);
            return 1;
        }

    }

    @Override
    public Reward getRewardDetail(int id) {

        return rewardMapper.getReward(id);
    }

    @Override
    public List<ClientCreditRecordResponseVo> getCreditRecord(String userId) {
        // 1.获取用户获取积分的记录
        List<ClientCreditRecordResponseVo> creditIn = rewardMapper.getCreditIn(userId);
        // 2.获取用户使用积分的记录
        List<ClientCreditRecordResponseVo> creditOut = rewardMapper.getCreditOut(userId);
        // 3.将结果按时间排序
        creditIn.addAll(creditOut);
        creditIn.sort((o1, o2) -> o2.getTime().compareTo(o1.getTime()));
        return creditIn;
    }


}
